package com.invite.me.beans.presenter;

import android.util.Log;


import com.google.gson.Gson;
import com.invite.me.api.UrlConstants;
import com.invite.me.beans.fabean.RegisterDlFaBean;
import com.invite.me.beans.fabean.YanZhengmaFaBean;
import com.invite.me.beans.res.DiTuWeiZhiFaBean;
import com.invite.me.beans.res.NameattestationBean;
import com.invite.me.beans.res.YanZhenMaShouBean;
import com.invite.me.beans.res.YanZhenmaDlShouBean;
import com.invite.me.ui.registeractivity.RegisterActivity;
import com.invite.me.util.SspPrefUtils;
import com.invite.me.wlqq.Constants;
import com.invite.me.wlqq.Contract;
import com.invite.me.wlqq.RxJavaDataImpl;

import java.io.IOException;
import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class RegisterPresenter implements Contract.BasePresenter {
    RegisterActivity splashActivity;

    public RegisterPresenter(RegisterActivity signInActivity) {
        this.splashActivity = signInActivity;
        splashActivity.setPresenter(this);
    }

    @Override
    public void start() {

    }
    //获取验证码
    public void yanzhengma(String phone,String type){

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(Constants.HEADER_KEY, Constants.HEADER_VALUE);
        YanZhengmaFaBean logeInDataBean = new YanZhengmaFaBean();
        logeInDataBean.setPhone(phone);
        logeInDataBean.setType(type);
        logeInDataBean.setSituation(2);

        Gson gson = new Gson();
        String phoneNumber = gson.toJson(logeInDataBean);
        RequestBody requestBody = RxJavaDataImpl.getRequestBody(phoneNumber);
        RxJavaDataImpl.postData(UrlConstants.API_YZM, hashMap, requestBody, new Observer() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object value) {
                if (value instanceof ResponseBody) {
                    try {
                        ResponseBody responseBody = (ResponseBody) value;
                        String str = responseBody.string();
                        Gson gson = new Gson();
                        YanZhenMaShouBean gankInfo = gson.fromJson(str, YanZhenMaShouBean.class);

                        splashActivity.onDataSucess(gankInfo);


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                Log.e("onerror", e+"" );
                splashActivity.onDataFaile(e);
            }

            @Override
            public void onComplete() {
                splashActivity.onComplete();
            }
        });
    }

    //验证码登录
    public void denglu(String phone,String yanzhengma){

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(Constants.HEADER_KEY, Constants.HEADER_VALUE);
        RegisterDlFaBean logeInDataBean = new RegisterDlFaBean();
        logeInDataBean.setPhone(phone);
        logeInDataBean.setCode(yanzhengma);
        logeInDataBean.setSituation(1);


        Gson gson = new Gson();
        String phoneNumber = gson.toJson(logeInDataBean);
        RequestBody requestBody = RxJavaDataImpl.getRequestBody(phoneNumber);
        RxJavaDataImpl.postData(UrlConstants.API_YZDL, hashMap, requestBody, new Observer() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object value) {
                if (value instanceof ResponseBody) {
                    try {
                        ResponseBody responseBody = (ResponseBody) value;
                        String str = responseBody.string();
                        Gson gson = new Gson();
                        YanZhenmaDlShouBean gankInfo = gson.fromJson(str, YanZhenmaDlShouBean.class);

                        splashActivity.onDataSucess(gankInfo);


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                Log.e("onerror", e+"" );
                splashActivity.onDataFaile(e);
            }

            @Override
            public void onComplete() {
                splashActivity.onComplete();
            }
        });
    }


    //更新地理位置
    public void tijiaoshimingrenzheng(String diqu,String quyu,String chengshi,String weidu,String jingdu,String sheng,String jiedao){

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(Constants.HEADER_KEY, Constants.HEADER_VALUE);
        hashMap.put("userId", SspPrefUtils.getString(Constants.SSPTOKEN, ""));
        DiTuWeiZhiFaBean logeInDataBean = new DiTuWeiZhiFaBean();
        logeInDataBean.setAddress(diqu);
        logeInDataBean.setArea(quyu);
        logeInDataBean.setCity(chengshi);
        logeInDataBean.setLatitude(weidu);
        logeInDataBean.setLongitude(jingdu);
        logeInDataBean.setProvince(sheng);
        logeInDataBean.setStreet(jiedao);

        Gson gson = new Gson();
        String phoneNumber = gson.toJson(logeInDataBean);
        RequestBody requestBody = RxJavaDataImpl.getRequestBody(phoneNumber);
        RxJavaDataImpl.putData(UrlConstants.API_TJSMRZ, hashMap, requestBody,new Observer() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object value) {
                if (value instanceof ResponseBody) {
                    try {
                        ResponseBody responseBody = (ResponseBody) value;
                        String str = responseBody.string();
                        Gson gson = new Gson();
                        NameattestationBean gankInfo = gson.fromJson(str, NameattestationBean.class);
                        splashActivity.onDataSucess(gankInfo);


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                Log.e("onerror", e+"" );
                splashActivity.onDataFaile(e);
            }

            @Override
            public void onComplete() {
                splashActivity.onComplete();
            }
        });
    }
}
